package com.weixx.yctp.cp.service;

import cc.eamon.open.status.StatusException;
import com.weixx.yctp.cp.dao.CpDao;
import com.weixx.yctp.cp.entity.Cp;
import com.weixx.yctp.cp.entity.CpDetailMapper;
import com.weixx.yctp.cp.entity.CpExample;
import com.weixx.yctp.cp.entity.CpPostMapper;
import com.weixx.yctp.cp.entity.CpSimpleMapper;
import com.weixx.yctp.cp.entity.CpUpdateMapper;
import java.lang.Byte;
import java.lang.Integer;
import java.lang.Long;
import java.lang.Object;
import java.lang.String;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Author: eamon
 * Email: eamon@eamon.cc */
@Service
public class CpService {
  @Autowired
  private CpDao cpDao;

  /**
   * 分页计数函数 */
  private CpExample initPageRowQueryFilter(Long page, Integer rows) {
    CpExample example = new CpExample();
    example.setOrderByClause("updateTime desc");
    CpExample.Criteria criteria = example.createCriteria();
    criteria.andIsDeleteEqualTo((byte) 0);
    if (page > 0) {
      Long offset = (page - 1L) * rows;
      example.setLimit(rows);
      example.setOffset(offset);
    }
    return example;
  }

  /**
   * 参数过滤函数 */
  private CpExample initQueryFilter(Long page, Integer rows, Integer id, Integer sfgw,
      String sfgw_BZ, Integer sjxj, String sjxj_BZ, Integer sczj, String sczj_BZ, Integer sscjgj,
      String sscjgj_BZ, Integer szrzyhghj, String szrzyhghj_BZ, Integer slyhylj, String slyhylj_BZ,
      Integer snyncj, String snyncj_BZ, Integer szwfwhdsjglj, String szwfwhdsjglj_BZ,
      Integer ssthjj, String ssthjj_BZ, Integer syjglj, String syjglj_BZ, Integer sslhpj,
      String sslhpj_BZ, Integer swhhlyj, String swhhlyj_BZ, Integer szjj, String szjj_BZ,
      Integer sgdgs, String sgdgs_BZ, Integer sxfjyzd, String sxfjyzd_BZ, Integer srfb,
      String srfb_BZ, Integer sqxj, String sqxj_BZ, Integer ycsxswyxgs, String ycsxswyxgs_BZ,
      Integer yczrcsrqyxgs, String yczrcsrqyxgs_BZ, String ipAddress, String mark, String markName,
      String pcTime, Date createTime, String createBy, Date updateTime, String updateBy,
      Byte isDelete, String expand1field, String expand2field, String expand3field,
      String expand4field, String expand5field) {
    CpExample example = new CpExample();
    example.setOrderByClause("updateTime desc");
    CpExample.Criteria criteria = example.createCriteria();
    if(id!=null) {
      criteria.andIdEqualTo(id);
    }
    if(sfgw!=null) {
      criteria.andSfgwEqualTo(sfgw);
    }
    if(sfgw_BZ!=null && !sfgw_BZ.equals("")) {
      criteria.andSfgw_BZEqualTo(sfgw_BZ);
    }
    if(sjxj!=null) {
      criteria.andSjxjEqualTo(sjxj);
    }
    if(sjxj_BZ!=null && !sjxj_BZ.equals("")) {
      criteria.andSjxj_BZEqualTo(sjxj_BZ);
    }
    if(sczj!=null) {
      criteria.andSczjEqualTo(sczj);
    }
    if(sczj_BZ!=null && !sczj_BZ.equals("")) {
      criteria.andSczj_BZEqualTo(sczj_BZ);
    }
    if(sscjgj!=null) {
      criteria.andSscjgjEqualTo(sscjgj);
    }
    if(sscjgj_BZ!=null && !sscjgj_BZ.equals("")) {
      criteria.andSscjgj_BZEqualTo(sscjgj_BZ);
    }
    if(szrzyhghj!=null) {
      criteria.andSzrzyhghjEqualTo(szrzyhghj);
    }
    if(szrzyhghj_BZ!=null && !szrzyhghj_BZ.equals("")) {
      criteria.andSzrzyhghj_BZEqualTo(szrzyhghj_BZ);
    }
    if(slyhylj!=null) {
      criteria.andSlyhyljEqualTo(slyhylj);
    }
    if(slyhylj_BZ!=null && !slyhylj_BZ.equals("")) {
      criteria.andSlyhylj_BZEqualTo(slyhylj_BZ);
    }
    if(snyncj!=null) {
      criteria.andSnyncjEqualTo(snyncj);
    }
    if(snyncj_BZ!=null && !snyncj_BZ.equals("")) {
      criteria.andSnyncj_BZEqualTo(snyncj_BZ);
    }
    if(szwfwhdsjglj!=null) {
      criteria.andSzwfwhdsjgljEqualTo(szwfwhdsjglj);
    }
    if(szwfwhdsjglj_BZ!=null && !szwfwhdsjglj_BZ.equals("")) {
      criteria.andSzwfwhdsjglj_BZEqualTo(szwfwhdsjglj_BZ);
    }
    if(ssthjj!=null) {
      criteria.andSsthjjEqualTo(ssthjj);
    }
    if(ssthjj_BZ!=null && !ssthjj_BZ.equals("")) {
      criteria.andSsthjj_BZEqualTo(ssthjj_BZ);
    }
    if(syjglj!=null) {
      criteria.andSyjgljEqualTo(syjglj);
    }
    if(syjglj_BZ!=null && !syjglj_BZ.equals("")) {
      criteria.andSyjglj_BZEqualTo(syjglj_BZ);
    }
    if(sslhpj!=null) {
      criteria.andSslhpjEqualTo(sslhpj);
    }
    if(sslhpj_BZ!=null && !sslhpj_BZ.equals("")) {
      criteria.andSslhpj_BZEqualTo(sslhpj_BZ);
    }
    if(swhhlyj!=null) {
      criteria.andSwhhlyjEqualTo(swhhlyj);
    }
    if(swhhlyj_BZ!=null && !swhhlyj_BZ.equals("")) {
      criteria.andSwhhlyj_BZEqualTo(swhhlyj_BZ);
    }
    if(szjj!=null) {
      criteria.andSzjjEqualTo(szjj);
    }
    if(szjj_BZ!=null && !szjj_BZ.equals("")) {
      criteria.andSzjj_BZEqualTo(szjj_BZ);
    }
    if(sgdgs!=null) {
      criteria.andSgdgsEqualTo(sgdgs);
    }
    if(sgdgs_BZ!=null && !sgdgs_BZ.equals("")) {
      criteria.andSgdgs_BZEqualTo(sgdgs_BZ);
    }
    if(sxfjyzd!=null) {
      criteria.andSxfjyzdEqualTo(sxfjyzd);
    }
    if(sxfjyzd_BZ!=null && !sxfjyzd_BZ.equals("")) {
      criteria.andSxfjyzd_BZEqualTo(sxfjyzd_BZ);
    }
    if(srfb!=null) {
      criteria.andSrfbEqualTo(srfb);
    }
    if(srfb_BZ!=null && !srfb_BZ.equals("")) {
      criteria.andSrfb_BZEqualTo(srfb_BZ);
    }
    if(sqxj!=null) {
      criteria.andSqxjEqualTo(sqxj);
    }
    if(sqxj_BZ!=null && !sqxj_BZ.equals("")) {
      criteria.andSqxj_BZEqualTo(sqxj_BZ);
    }
    if(ycsxswyxgs!=null) {
      criteria.andYcsxswyxgsEqualTo(ycsxswyxgs);
    }
    if(ycsxswyxgs_BZ!=null && !ycsxswyxgs_BZ.equals("")) {
      criteria.andYcsxswyxgs_BZEqualTo(ycsxswyxgs_BZ);
    }
    if(yczrcsrqyxgs!=null) {
      criteria.andYczrcsrqyxgsEqualTo(yczrcsrqyxgs);
    }
    if(yczrcsrqyxgs_BZ!=null && !yczrcsrqyxgs_BZ.equals("")) {
      criteria.andYczrcsrqyxgs_BZEqualTo(yczrcsrqyxgs_BZ);
    }
    if(ipAddress!=null && !ipAddress.equals("")) {
      criteria.andIpAddressEqualTo(ipAddress);
    }
    if(mark!=null && !mark.equals("")) {
      criteria.andMarkEqualTo(mark);
    }
    if(markName!=null && !markName.equals("")) {
      criteria.andMarkNameEqualTo(markName);
    }
    if(pcTime!=null && !pcTime.equals("")) {
      criteria.andPcTimeEqualTo(pcTime);
    }
    if(createTime!=null) {
      criteria.andCreateTimeEqualTo(createTime);
    }
    if(createBy!=null && !createBy.equals("")) {
      criteria.andCreateByEqualTo(createBy);
    }
    if(updateTime!=null) {
      criteria.andUpdateTimeEqualTo(updateTime);
    }
    if(updateBy!=null && !updateBy.equals("")) {
      criteria.andUpdateByEqualTo(updateBy);
    }
    if(isDelete!=null) {
      criteria.andIsDeleteEqualTo(isDelete);
    }
    if(expand1field!=null && !expand1field.equals("")) {
      criteria.andExpand1fieldEqualTo(expand1field);
    }
    if(expand2field!=null && !expand2field.equals("")) {
      criteria.andExpand2fieldEqualTo(expand2field);
    }
    if(expand3field!=null && !expand3field.equals("")) {
      criteria.andExpand3fieldEqualTo(expand3field);
    }
    if(expand4field!=null && !expand4field.equals("")) {
      criteria.andExpand4fieldEqualTo(expand4field);
    }
    if(expand5field!=null && !expand5field.equals("")) {
      criteria.andExpand5fieldEqualTo(expand5field);
    }
    if (page > 0) {
      Long offset = (page - 1L) * rows;
      example.setLimit(rows);
      example.setOffset(offset);
    }
    return example;
  }

  public Long getCpCount() throws StatusException {
    return cpDao.countByExample(initPageRowQueryFilter(0L, 0));
  }

  public Long getCpFilterCount(Integer id, Integer sfgw, String sfgw_BZ, Integer sjxj,
      String sjxj_BZ, Integer sczj, String sczj_BZ, Integer sscjgj, String sscjgj_BZ,
      Integer szrzyhghj, String szrzyhghj_BZ, Integer slyhylj, String slyhylj_BZ, Integer snyncj,
      String snyncj_BZ, Integer szwfwhdsjglj, String szwfwhdsjglj_BZ, Integer ssthjj,
      String ssthjj_BZ, Integer syjglj, String syjglj_BZ, Integer sslhpj, String sslhpj_BZ,
      Integer swhhlyj, String swhhlyj_BZ, Integer szjj, String szjj_BZ, Integer sgdgs,
      String sgdgs_BZ, Integer sxfjyzd, String sxfjyzd_BZ, Integer srfb, String srfb_BZ,
      Integer sqxj, String sqxj_BZ, Integer ycsxswyxgs, String ycsxswyxgs_BZ, Integer yczrcsrqyxgs,
      String yczrcsrqyxgs_BZ, String ipAddress, String mark, String markName, String pcTime,
      Date createTime, String createBy, Date updateTime, String updateBy, Byte isDelete,
      String expand1field, String expand2field, String expand3field, String expand4field,
      String expand5field) throws StatusException {
    return cpDao.countByExample(initQueryFilter(0L, 0, id, sfgw, sfgw_BZ, sjxj, sjxj_BZ, sczj, sczj_BZ, sscjgj, sscjgj_BZ, szrzyhghj, szrzyhghj_BZ, slyhylj, slyhylj_BZ, snyncj, snyncj_BZ, szwfwhdsjglj, szwfwhdsjglj_BZ, ssthjj, ssthjj_BZ, syjglj, syjglj_BZ, sslhpj, sslhpj_BZ, swhhlyj, swhhlyj_BZ, szjj, szjj_BZ, sgdgs, sgdgs_BZ, sxfjyzd, sxfjyzd_BZ, srfb, srfb_BZ, sqxj, sqxj_BZ, ycsxswyxgs, ycsxswyxgs_BZ, yczrcsrqyxgs, yczrcsrqyxgs_BZ, ipAddress, mark, markName, pcTime, createTime, createBy, updateTime, updateBy, isDelete, expand1field, expand2field, expand3field, expand4field, expand5field));
  }

  public Cp getCpByPrimaryKey(Integer key) throws StatusException {
    return cpDao.selectByPrimaryKey(key);
  }

  public Map<String, Object> getCpSimpleMapByPrimaryKey(Integer key) throws StatusException {
    Cp entity = cpDao.selectByPrimaryKey(key);
    if(entity == null) throw new StatusException("ENTITY_NULL");
    return CpSimpleMapper.getMap(entity);
  }

  public Map<String, Object> getCpDetailMapByPrimaryKey(Integer key) throws StatusException {
    Cp entity = cpDao.selectByPrimaryKey(key);
    if(entity == null) throw new StatusException("ENTITY_NULL");
    return CpDetailMapper.getMap(entity);
  }


  //通过多个主键查询
  public List<Map<String,Object>> getCpDetailMapByPrimaryKeyList(List<String> pcTimes) throws StatusException {
    List<Map<String,Object>> entityMapList=new ArrayList<>();
    for (String pcTime : pcTimes) {
      List<Map<String, Object>> maps = queryByPc(pcTime, (long) -1, 10);
      for (Map<String, Object> map : maps) {
        entityMapList.add(map);
      }
    }
    return entityMapList;
  }

  public List<Map<String, Object>> getCpSimpleMapList(Long page, Integer rows) throws
      StatusException {
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    for (Cp entity: cpDao.selectByExample(initPageRowQueryFilter(page, rows))) {
      entityMapList.add(CpSimpleMapper.getMap(entity));
    }
    return entityMapList;
  }

  public List<Map<String, Object>> getCpDetailMapList(Long page, Integer rows) throws
      StatusException {
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    for (Cp entity: cpDao.selectByExample(initPageRowQueryFilter(page, rows))) {
      entityMapList.add(CpDetailMapper.getMap(entity));
    }
    return entityMapList;
  }

  public Map<String, Object> postCp(CpPostMapper postMapper, String createBy, String updateBy,String ip)
      throws StatusException {
    // 记录上传条目数;
    AtomicInteger count = new AtomicInteger();
    // 从上传数据中拿出记录;
    Cp entity = postMapper.getEntity();
    //设置ip
    entity.setIpAddress(ip);
    // 记录创建者;
    entity.setCreateBy(createBy);
    // 记录更新者;
    entity.setUpdateBy(updateBy);
    // 持久化对象;
    try {
      count.addAndGet(cpDao.insertSelective(entity));
    }
    catch (Exception e) {
      throw new StatusException("ENTITY_EXIST");
    }
    return CpDetailMapper.getMap(entity);
  }

  public List<Map<String, Object>> postCpList(List<CpPostMapper> postMappers, String createBy,
      String updateBy,String ip) throws StatusException {
    // 记录上传条目数; o
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    // 将List中所有的Entity均上传;
    for (CpPostMapper postMapper: postMappers) {
      // 插入计数;
      entityMapList.add(postCp(postMapper, createBy, updateBy,ip));
    }
    return entityMapList;
  }

  public Map<String, Object> updateCp(CpUpdateMapper updateMapper, String updateBy) throws
      StatusException {
    // 记录更新条目数;
    AtomicInteger count = new AtomicInteger();
    // 从更新数据中拿出记录;
    Cp entity = updateMapper.getEntity();
    // 记录更新者;
    entity.setUpdateBy(updateBy);
    try {
      count.addAndGet(cpDao.updateByPrimaryKeySelective(entity));
    }
    catch (Exception e) {
      throw new StatusException("ENTITY_NULL");
    }
    return CpDetailMapper.getMap(entity);
  }

  public List<Map<String, Object>> updateCpList(List<CpUpdateMapper> updateMappers, String updateBy)
      throws StatusException {
    // 记录更新条目数;
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    // 将List中所有的Entity更新;
    for (CpUpdateMapper updateMapper: updateMappers) {
      // 更新计数;
      entityMapList.add(updateCp(updateMapper, updateBy));
    }
    return entityMapList;
  }

  public Integer deleteCp(Integer key, String updateBy) throws StatusException {
    // 记录更新条目数;
    AtomicInteger count = new AtomicInteger();
    // 从更新数据中拿出记录;
    Cp entity = new Cp();
    // 记录更新者;
    entity.setId(key);
    entity.setUpdateBy(updateBy);
    // 删除条目;
    entity.setIsDelete((byte) 1);
    count.addAndGet(cpDao.updateByPrimaryKeySelective(entity));
    return count.get();
  }

  public Integer deleteCpList(List<Integer> keys, String updateBy) throws StatusException {
    // 记录更新条目数;
    AtomicInteger count = new AtomicInteger();
    // 将List中所有的Entity更新;
    for (Integer key: keys) {
      // 更新计数;
      count.addAndGet(deleteCp(key, updateBy));
    }
    return count.get();
  }

  public Integer recoverCp(Integer key, String updateBy) throws StatusException {
    // 记录更新条目数;
    AtomicInteger count = new AtomicInteger();
    // 从更新数据中拿出记录;
    Cp entity = new Cp();
    // 记录更新者;
    entity.setId(key);
    entity.setUpdateBy(updateBy);
    // 恢复条目;
    entity.setIsDelete((byte) 0);
    count.addAndGet(cpDao.updateByPrimaryKeySelective(entity));
    return count.get();
  }

  public Integer recoverCpList(List<Integer> keys, String updateBy) throws StatusException {
    // 记录更新条目数;
    AtomicInteger count = new AtomicInteger();
    // 将List中所有的Entity更新;
    for (Integer key: keys) {
      // 更新计数;
      count.addAndGet(recoverCp(key, updateBy));
    }
    return count.get();
  }

  //根据时间查询测评记录
  public List<Map<String, Object>> queryByTime(Date startDate,Date endDate,Long page, Integer rows) throws StatusException {
    CpExample example = new CpExample();
    example.setOrderByClause("updateTime desc");
    CpExample.Criteria criteria = example.createCriteria();
    criteria.andIsDeleteEqualTo((byte) 0).andCreateTimeBetween(startDate,endDate);
    if (page > 0) {
      Long offset = (page - 1L) * rows;
      example.setLimit(rows);
      example.setOffset(offset);
    }
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    for (Cp entity: cpDao.selectByExample(example)) {
      entityMapList.add(CpDetailMapper.getMap(entity));
    }
    return entityMapList;
  }


  //根据批次查询测评记录
  public List<Map<String, Object>> queryByPc(String Pc,Long page, Integer rows) throws StatusException {
    CpExample example = new CpExample();
    example.setOrderByClause("updateTime desc");
    CpExample.Criteria criteria = example.createCriteria();
    criteria.andIsDeleteEqualTo((byte) 0).andPcTimeEqualTo(Pc);
    if (page > 0) {
      Long offset = (page - 1L) * rows;
      example.setLimit(rows);
      example.setOffset(offset);
    }
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    for (Cp entity: cpDao.selectByExample(example)) {
      entityMapList.add(CpDetailMapper.getMap(entity));
    }
    return entityMapList;
  }
  //多个批次  批量查询
  //根据批次查询测评记录
  public Map<String,List<Map<String, Object>>> queryByPcMutil(List<String> Pc,Long page, Integer rows) throws StatusException {
    CpExample example = new CpExample();
    example.setOrderByClause("updateTime desc");
    CpExample.Criteria criteria = example.createCriteria();
    Map<String,List<Map<String, Object>>> MutilList=new HashMap<>();
    for (String s : Pc) {
      criteria.andIsDeleteEqualTo((byte) 0).andPcTimeEqualTo(s);
      if (page > 0) {
        Long offset = (page - 1L) * rows;
        example.setLimit(rows);
        example.setOffset(offset);
      }
      List<Map<String, Object>> entityMapList = new ArrayList<>();
      for (Cp entity: cpDao.selectByExample(example)) {
        entityMapList.add(CpDetailMapper.getMap(entity));
      }
      MutilList.put(s,entityMapList);
    }
    //返回多个批次的entitylist
    return MutilList;
  }


  //根据批次获取平均情况
  public List<Map.Entry<String, Integer>> queryByPcAverage(String pcTime) throws StatusException {
    CpExample example = new CpExample();
    example.setOrderByClause("updateTime desc");
    CpExample.Criteria criteria = example.createCriteria();
    criteria.andIsDeleteEqualTo((byte) 0).andPcTimeEqualTo(pcTime);
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    Long count= cpDao.countByExample(example);
    if (count==0){
      throw new RuntimeException("未查询到记录");
    }
    for (Cp entity: cpDao.selectByExample(example)) {
      entityMapList.add(CpDetailMapper.getMap(entity));
    }
    //获得了该批次的所有map，19个单位排序
    /**
     *       AVG(sfgw), AVG(sjxj), AVG(sczj), AVG(sscjgj), AVG(szrzyhghj),
     *      AVG(slyhylj),  AVG(snyncj),  AVG(szwfwhdsjglj),  AVG(ssthjj),
     *       AVG(syjglj),  AVG(sslhpj),  AVG(swhhlyj),  AVG(szjj), AVG(sgdgs),
     *        AVG(sxfjyzd),  AVG(srfb), AVG(sqxj),  AVG(ycsxswyxgs),
     *        AVG(yczrcsrqyxgs)
     */
    String[] government={"sfgw","sjxj","sczj","sscjgj","szrzyhghj","slyhylj","snyncj","szwfwhdsjglj","ssthjj","syjglj",
    "sslhpj","swhhlyj","szjj","sgdgs","sxfjyzd","srfb","sqxj","ycsxswyxgs","yczrcsrqyxgs"};
    Map<String,Integer> gSorce=new HashMap<>();
    for (String s : government) {
      gSorce.put(s,0);
    }
    for (String s : gSorce.keySet()) {
      for (Map<String, Object> stringObjectMap : entityMapList) {
        gSorce.put(s,(gSorce.get(s)+(Integer) stringObjectMap.get(s)));

        }
      }
    for (String s : government) {
      gSorce.put(s,gSorce.get(s)/Integer.parseInt(String.valueOf(count)));
    }


    //自定义比较器
    Comparator<Map.Entry<String, Integer>> valCmp = new Comparator<Map.Entry<String, Integer>>() {
      @Override
      public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
        // TODO Auto-generated method stub
        return (int) (o2.getValue()-o1.getValue());  // 降序排序，如果想升序就反过来
      }
    };
    //将map转成List，map的一组key，value对应list一个存储空间
    List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String,Integer>>(gSorce.entrySet()); //传入maps实体
    Collections.sort(list,valCmp); // 注意此处Collections 是java.util包下面的,传入List和自定义的valCmp比较器
    //输出map
    return list;


  }
  //查询未标记的测评记录
  public List<Map<String,Object>> getCpwithNoMark(Long page,Integer rows){
    CpExample example = new CpExample();
    example.setOrderByClause("updateTime desc");
    CpExample.Criteria criteria = example.createCriteria();
    criteria.andIsDeleteEqualTo((byte) 0).andMarkEqualTo("");
    if (page > 0) {
      Long offset = (page - 1L) * rows;
      example.setLimit(rows);
      example.setOffset(offset);
    }
    List<Map<String, Object>> entityMapList = new ArrayList<>();
    Long count= cpDao.countByExample(example);
    for (Cp entity: cpDao.selectByExample(example)) {
      entityMapList.add(CpDetailMapper.getMap(entity));
    }

    return entityMapList;

  }


}
